#ifndef _AVR_ANALOG_COMPARATOR_H_
#define _AVR_ANALOG_COMPARATOR_H_

#include "../../Component.h"
#include<avr/io.h>
#include<avr/interrupt.h>
#include "../../../bit/bit.h"

namespace Hardware
{
namespace Avr
{

	typedef struct ANALOG_COMPARATOR_CONFIG
	{
		unsigned char analogCompDisable : 1;
		unsigned char bandgapSelect : 1;
		unsigned char interupt : 1;
		unsigned char captureEnable : 1;
		unsigned char interruptMode : 2;
		unsigned char multiplexInput : 5;

	} AnalogComparatorConfig;

	typedef struct ANALOG_COMPARATOR_DATA
	{
		unsigned char output : 1;
	} AnalogComparatorData;

	class AnalogComparator : public Component<AnalogComparatorConfig,
						  AnalogComparatorData>
	{
		public :
		enum ComparatorMultiplexInput
		{
			AIN1_1 = 0x00,
			AIN1_2 = 0x18,
			ADC0 = 0x10,
			ADC1 = 0x11,
			ADC2 = 0x12,
			ADC3 = 0x13,
			ADC4 = 0x14,
			ADC5 = 0x15,
			ADC6 = 0x16,
			ADC7 = 0x17
		};

		enum InterruptModeSelect
		{
			InterruptOnOutputToggle=0x00,
			Reserved1=0x01,
			InterruptOnFalling=0x02,
			InterruptOnRising=0x03
		};
		AnalogComparator(){}
		AnalogComparator(AnalogComparatorConfig c){configuration=c;}
		void config()
		{
			//set analog disable
			ACSR = SetBit(ACSR, ACD, configuration.analogCompDisable); 
			//set bandgap select
			ACSR = SetBit(ACSR, ACBG, configuration.bandgapSelect);
			//set interrupt enable
			ACSR = SetBit(ACSR, ACIE, configuration.interrupt);
			//set capture enable
			ACSR = SetBit(ACSR, ACIC, configuration.captureEnable);
			//set interrupt mode
			ACSR = (ACSR & ~(0x03)) | configuration.interruptMode;
			
			//set multiplex input now
			ACSR = SetBit(ACSR, ACME, configuration.multiplexInput>>4);
			ACSR = SetBit(ACSR, ADEN, (configuration.multiplexInput&0x0F)
							>>3);
			ADMUX = (ADMUX & ~0x07) 
				| (configuration.multiplexInput&0x07);
			
		}
		void run(){}
	};
}
}




#endif
